//官方圈子-模板

//引入组件库
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var circleUrl = require("url/circle");
var http = require("helper/http");
var userUtil = require("helper/user");
var common = require("common");

var alayoutRoot = ui("alayoutRoot");
var lableGrade = ui("lableGrade");
var buttonStop = ui("buttonStop");
//声明UI变量
var root=ui("$");  //$表示当前视图的根UI

root.setMapping({
	"alayoutRoot.tag" : "circleId",
	"buttonStop.tag" : "isUsed",
	"lableGrade.tag" : "gradeCode"
});

root.on("dataRefreshed", function(){
	resetBtnStatus();
});

//重置 按钮状态
function resetBtnStatus(){
	var isUsed = buttonStop.tag;//是否启用:1-是 2-否
	if(isUsed=="1"){//已启用
		buttonStop.text = "停用";
		buttonStop.bgColor = "808080FF";
	}else if(isUsed=="2"){//未启用
		buttonStop.text = "启用";
		buttonStop.bgColor = "FF7F00FF";
	}
}

var buttonStop = ui("buttonStop");
buttonStop.on("touch", function(data, e) {
	if(userUtil.loginToken()===""){
		nf.alert("您还没有登录哦.");
	}else{
		var circleId = alayoutRoot.tag;
		var isUsed = buttonStop.tag;//是否启用:1-是 2-否
		var gradeCode = lableGrade.tag;
		
		if(isUsed=="1"){//已启用
			http.post(circleUrl.url.memberGrade.setGradeDisable,{"circleId":circleId,"gradeCode":gradeCode},function(data){
				nf.toast("已停用");
				buttonStop.tag = "2";
				resetBtnStatus();
				page.fire("memberGrade_update", gradeCode);
			});
		}else if(isUsed=="2"){//未启用
			http.post(circleUrl.url.memberGrade.setGradeEnable,{"circleId":circleId,"gradeCode":gradeCode},function(data){
				nf.toast("已启用");
				buttonStop.tag = "1";
				resetBtnStatus();
				page.fire("memberGrade_update", gradeCode);
			});
		}
		
	}
})
